package com.quasar.hpatient.module.discover_community;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.discover_community.CommunityMulitModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BasePresenter;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonMultAdapter;
import lib.quasar.recycler.decoration.SpaceDecoration;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.recycler.model.MultModel;
import lib.quasar.util.DimenUtil;

/**
 * description: 发现 ==> 社区
 * created by kalu on 2018/3/19 16:14
 */
public class DiscoverCommunityPresenter implements BasePresenter {

    private final ArrayList<CommunityMulitModel> mData = new ArrayList<>();

    {
        CommunityMulitModel model1 = new CommunityMulitModel();
        model1.setMultType(MultModel.TYPE_1);
        mData.add(model1);

        CommunityMulitModel model2 = new CommunityMulitModel();
        model2.setMultType(MultModel.TYPE_2);
        mData.add(model2);

        CommunityMulitModel model3 = new CommunityMulitModel();
        model3.setMultType(MultModel.TYPE_3);
        mData.add(model3);

        CommunityMulitModel model4 = new CommunityMulitModel();
        model4.setMultType(MultModel.TYPE_4);
        mData.add(model4);
    }

    void initList(final DiscoverCommunityView view, RecyclerView recycler) {

        if (null == view || null == recycler) return;

        // 适配器
        final BaseCommonMultAdapter<CommunityMulitModel> adapter = new BaseCommonMultAdapter<CommunityMulitModel>(mData) {

            @Override
            protected void onMult() {
                addMult(MultModel.TYPE_1, R.layout.fragment_discover_community_type1);
                addMult(MultModel.TYPE_2, R.layout.fragment_discover_community_type2);
                addMult(MultModel.TYPE_3, R.layout.fragment_discover_community_type3);
                addMult(MultModel.TYPE_4, R.layout.fragment_discover_community_type4);
            }

            @Override
            protected void onNext(RecyclerHolder holder, CommunityMulitModel model, int position) {
                switch (holder.getItemViewType()) {
                    case MultModel.TYPE_2:

                        final ImageView image1 = holder.getView(R.id.fragment_discover_community_type2_image1);
                        final ImageView image2 = holder.getView(R.id.fragment_discover_community_type2_image2);
                        final ImageView image3 = holder.getView(R.id.fragment_discover_community_type2_image3);

                        CommunityMulitModel.CommunityImageModel photo1 = new CommunityMulitModel.CommunityImageModel();
                        photo1.setUrl("http://scimg.jb51.net/allimg/160905/2-160Z51P540H0.gif");

                        CommunityMulitModel.CommunityImageModel photo2 = new CommunityMulitModel.CommunityImageModel();
                        photo2.setUrl("https://b-ssl.duitang.com/uploads/item/201511/18/20151118131629_RenHN.thumb.700_0.jpeg");

                        CommunityMulitModel.CommunityImageModel photo3 = new CommunityMulitModel.CommunityImageModel();
                        photo3.setUrl("http://img-download.pchome.net/download/1k0/h1/4j/o4jbrz-fkz.jpg@0e_0o_1024w_768h_90q.src");

                        view.setImage(holder, R.id.fragment_discover_community_type2_image1, photo1.getUrl());
                        view.setImage(holder, R.id.fragment_discover_community_type2_image2, photo2.getUrl());
                        view.setImage(holder, R.id.fragment_discover_community_type2_image3, photo3.getUrl());

                        holder.setOnClickListener(R.id.fragment_discover_community_type2_image1, v -> {
                            List<ImageView> imageViews = Arrays.asList(image1, image2, image3);
                            List<CommunityMulitModel.CommunityImageModel> little = Arrays.asList(photo1, photo2, photo3);
                            List<CommunityMulitModel.CommunityImageModel> big = Arrays.asList(photo1, photo2, photo3);
                            view.showPhoto(0, little, big, imageViews);
                        });

                        holder.setOnClickListener(R.id.fragment_discover_community_type2_image2, v -> {
                            List<ImageView> imageViews = Arrays.asList(image1, image2, image3);
                            List<CommunityMulitModel.CommunityImageModel> little = Arrays.asList(photo1, photo2, photo3);
                            List<CommunityMulitModel.CommunityImageModel> big = Arrays.asList(photo1, photo2, photo3);
                            view.showPhoto(1, little, big, imageViews);
                        });

                        holder.setOnClickListener(R.id.fragment_discover_community_type2_image3, v -> {
                            List<ImageView> imageViews = Arrays.asList(image1, image2, image3);
                            List<CommunityMulitModel.CommunityImageModel> little = Arrays.asList(photo1, photo2, photo3);
                            List<CommunityMulitModel.CommunityImageModel> big = Arrays.asList(photo1, photo2, photo3);
                            view.showPhoto(2, little, big, imageViews);
                        });
                        break;
                }
            }
        };

        final RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DimenUtil.dp2px(240));
        View head = LayoutInflater.from(BaseApp.getContext()).inflate(R.layout.fragment_discover_community_head, null);
        head.setLayoutParams(params);
        adapter.addHead(head);

        View loading = LayoutInflater.from(BaseApp.getContext()).inflate(R.layout.layout_recycler_loading, null);
        adapter.setNullView(loading);

        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        SpaceDecoration decoration = new SpaceDecoration(DimenUtil.dp2px(2));
        view.setRecycler(recycler, adapter, manager, decoration);
    }

    @Override
    public void recycler() {
        mData.clear();
    }
}